//
//  GifCell.m
//  Mango
//
//  Created by lanou3g on 16/5/7.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "GifCell.h"
#import <DALabeledCircularProgressView.h>

@interface GifCell ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UIButton *gifBtn;

@property (nonatomic, assign) NSInteger height;
@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *progressView;


@end


@implementation GifCell

- (UIImageView *)coverImageView {
    if (!_coverImageView) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectZero];
        [self.contentView insertSubview:imageView atIndex:0];
        _coverImageView = imageView;
    }
    return _coverImageView;
}

- (IBAction)shareBtn:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(gifCellDidClickShareBtn:)]) {
        [self.delegate gifCellDidClickShareBtn:self.model];
    }
}

- (IBAction)gifAction:(UIButton *)sender {
    sender.alpha = 0;
     [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.model.thumb] placeholderImage:[UIImage imageNamed:@"placeholder"]options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
         self.progressView.hidden = NO;
         CGFloat progress = 1.0 * receivedSize / expectedSize;
         [self.progressView setProgress:progress animated:NO];//显示下载进度
     } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
         self.progressView.hidden = YES;
     }];
    
    if ([self.delegate respondsToSelector:@selector(gifCellDidClickGifBtn:)]) {
        [self.delegate gifCellDidClickGifBtn:self.indexPath];
    }
}

- (void)setModel:(GifModel *)model {
    if (_model != model) {
        _model = model;
        [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb_jpg]placeholderImage:[UIImage imageNamed:@"placeholder"]];
        self.titleLabel.text = model.title;
        
       
        self.height = model.height;
        self.coverImageView.frame = model.imageFrame;
    }
}

- (void)resetImageView {
    self.gifBtn.alpha = 1;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.model.thumb_jpg] placeholderImage:[UIImage imageNamed:@"placeholder"]];
}

- (void)setFrame:(CGRect)frame {
    frame.origin.y += 8;
    frame.size.height -= 8;
    [super setFrame:frame];
}

#pragma mark 切换至日间模式
-(void)updateToDaySkinMode {
    self.titleLabel.textColor = [UIColor blackColor];
    self.contentView.backgroundColor = [UIColor whiteColor];
}
#pragma mark 切换至夜间模式
-(void)updateToNightSkinMode {
    self.titleLabel.textColor = [UIColor grayColor];
    self.contentView.backgroundColor = [UIColor colorWithRed:42/255.0 green:39/255.0 blue:43/255.0 alpha:1.0];
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
